<template>
  <div class="login-container">
    <div class="login-box">
      <div class="avatar-box">
        <img src="../../assets/banklogo.svg" alt="" />
      </div>
      <el-form
        label-width="0px"
        class="login-form"
        :model="loginForm"
        :rules="loginFormRules"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="userpwd">
          <el-input
            v-model="loginForm.userpwd"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" class="btn" @click="login">登录</el-button>
          <el-button type="info" class="btn" style="margin-left: 50px"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { postLogin } from "../../network/home.js";
export default {
  data() {
    return {
      loginForm: {
        username: "tkc",
        userpwd: "123456",
      },
      // 验证规则
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        userpwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 5 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    login() {
      postLogin(this.loginForm.username, this.loginForm.userpwd).then((res) => {
        // console.log(this.loginForm.username)
        // console.log(res)
        if (res.status == 200) {
          this.$root.ADMIN.admin_id = res.data.admin_id
          this.$root.ADMIN.admin_name = res.data.admin_name
          this.$root.ADMIN.admin_num = res.data.admin_num
          this.$root.ADMIN.admin_pwd = res.data.admin_pwd

          window.sessionStorage.setItem("token", res.data.token)
          this.$router.replace("echarts");
        } else {
          alert(res.errMessage)
          // console.log(res.errMessage);
        }
      });
    },
  },
};
</script>

<style>
.login-container {
  background: #545c64;
  height: 100vh;
}
.login-box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar-box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}
.avatar-box img {
  width: 130px;
  height: 130px;
  border-radius: 50%;
  background-color: #eee;
}
.login-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: center;
}
.btn {
  width: 150px;
}
</style>